package com.yuejian_06.yuejian_06.fragment;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.yuejian_06.yuejian_06.Listener.MeListener;
import com.yuejian_06.yuejian_06.R;
import com.yuejian_06.yuejian_06.activity.AboutyuejianActivity;
import com.yuejian_06.yuejian_06.activity.FollowListActivity;
import com.yuejian_06.yuejian_06.activity.OrderActivity;
import com.yuejian_06.yuejian_06.activity.OrderListActivity;
import com.yuejian_06.yuejian_06.activity.PostListActivity;
import com.yuejian_06.yuejian_06.activity.SetpersonalActivity;
import com.yuejian_06.yuejian_06.bean.UserInfoBean;
import com.yuejian_06.yuejian_06.model.MeModel;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import static android.content.Context.MODE_PRIVATE;
import static com.yuejian_06.yuejian_06.activity.PostPicActivity.getTakePickIntent;

/**
 * Created by __ZL__ on 2017/9/25.
 */

public class MeFragment extends Fragment implements View.OnClickListener{
    private LinearLayout post;
    private LinearLayout follow;
    private LinearLayout order;
    private LinearLayout about;
    private LinearLayout set;
    private LinearLayout ph;

    private ImageView user_pic;
    private TextView user_name;

    private File mCurrentPhotoFile;// 照相机拍照得到的图片
    // 用来标识请求照相功能的activity
    private static final int CAMERA_WITH_DATA = 3023;

    // 用来标识请求gallery的activity
    private static final int PHOTO_PICKED_WITH_DATA = 3021;

    // 拍照的照片存储位置
    private static final File PHOTO_DIR = new File(
            Environment.getExternalStorageDirectory() + "/DCIM/Camera");
    private byte[] img; // 头像数据
    public MeFragment() {
        // Required empty public constructor

    }
    private MeListener<UserInfoBean> meListener =new MeListener<UserInfoBean>() {
        @Override
        public void onResponse(UserInfoBean o) {
            UserInfoBean res=o;
            user_name.setText(res.getUser_name());
            if (!"".equals(res.getUser_pic()))
                Picasso.with(getActivity()).load("http://10.0.2.2/yuejian/Public/userpics/"+res.getUser_pic()).into(user_pic);
        }
        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_me, container,false);
        post = (LinearLayout) view.findViewById(R.id.post);
        follow = (LinearLayout) view.findViewById(R.id.follow);
        order = (LinearLayout) view.findViewById(R.id.order);
        about = (LinearLayout) view.findViewById(R.id.about_yuejian);
        set = (LinearLayout) view.findViewById(R.id.set_personal);
        ph = (LinearLayout) view.findViewById(R.id.ph);


        user_pic = (ImageView) view.findViewById(R.id.user_pic);
        user_name = (TextView) view.findViewById(R.id.user_name);
        initEvent();
        SharedPreferences pref = getActivity().getSharedPreferences("userInfo",MODE_PRIVATE);
        String user_id = pref.getString("user_id","0");//第二个参数为默认值
        MeModel meModel = new MeModel();
        meModel.getUserInfo(user_id,meListener);
        return view;
    }
    private void initEvent(){
        post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),PostListActivity.class);
                startActivityForResult(intent,1);
            }
        });
        follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),FollowListActivity.class);
                startActivityForResult(intent,1);
            }
        });
        order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),OrderActivity.class);
                startActivityForResult(intent,1);
            }
        });
        about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),AboutyuejianActivity.class);
                startActivityForResult(intent,1);
            }
        });
        set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),SetpersonalActivity.class);
                startActivityForResult(intent,1);
            }
        });
        ph.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doTakePhoto();
            }
        });
    }

    @Override
    public void onClick(View v) {

    }

    protected void doTakePhoto() {
        try {
            // Launch camera to take photo for selected contact
            PHOTO_DIR.mkdirs();// 创建照片的存储目录
            mCurrentPhotoFile = new File(PHOTO_DIR, getPhotoFileName());// 给新照的照片文件命名
            final Intent intent = getTakePickIntent(mCurrentPhotoFile);
            startActivityForResult(intent, CAMERA_WITH_DATA);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(getActivity(), R.string.photoPickerNotFoundText,Toast.LENGTH_LONG).show();
        }
    }

    private String getPhotoFileName() {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
        return dateFormat.format(date) + ".png";
    }


}
